﻿#region Using Directives

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using Microsoft.VisualStudio.TeamArchitect.PowerTools.Features;
using VSLangProj;
using VsWebSite;

#endregion

namespace FeatureBuilder.ContribLib.Commands
{
    /// <summary>
    /// Feature Extension Command that does adds an assembly reference to a Project.
    /// </summary>
    [DisplayName("Add multiple Assembly References")]
    [Description("Adds an assembly reference to a Project.")]
    [Category("IDE")]
    public class AddAssembliesReference : FeatureCommand
    {
        public string FileNames { get; set; }
        public string AssembliesPath { get; set; }
        public EnvDTE.Project ReferringProject { get; set; }

        #region Overrides

        public override void Execute()
        {
            bool gaced = (string.IsNullOrEmpty(AssembliesPath));
            List<string> assemblies = BuildAssembliesPathList(gaced, FileNames, AssembliesPath);
            try
            {
                if (FeatureBuilder.ContribLib.Helpers.DteHelper.IsWebProject(ReferringProject))
                {
                    // This is a web project
                    VSWebSite webProject = ReferringProject.Object as VSWebSite;
                    if (webProject != null)
                    {
                        foreach (string assemblyFilePath in assemblies)
                        {
                            if (gaced)
                            {
                                webProject.References.AddFromGAC(assemblyFilePath);
                            }
                            else
                            {
                                webProject.References.AddFromFile(assemblyFilePath);
                            }
                        }
                    }
                }
                else
                {
                    // standard project
                    VSProject vsProject = ReferringProject.Object as VSProject;
                    if (vsProject != null)
                    {
                        foreach (string assemblyFilePath in assemblies)
                        {
                            vsProject.References.Add(assemblyFilePath);
                        }

                    }
                }
            }
            catch {}
        }

        #endregion

        #region privates

        private List<string> BuildAssembliesPathList(bool gaced, string fileNames, string assembliesPath)
        {
            List<string> _assembliesPath = new List<string>();

            if (gaced)
            {
                _assembliesPath.AddRange(fileNames.Split(';'));
            }
            else
            {
                GuardFolderExists(assembliesPath);

                foreach (string file in fileNames.Split(';'))
                {
                    string assemblyFilePath = Path.Combine(assembliesPath, file);

                    if (File.Exists(assemblyFilePath))
                    {
                        _assembliesPath.Add(assemblyFilePath);
                    }
                }
            }
            return _assembliesPath;
        }

        private void GuardFolderExists(string folder)
        {
            if (!Directory.Exists(folder))
            {
                throw new InvalidOperationException(
                    String.Format(Properties.Resources.FolderDoesNotExist, folder));
            }
        }

        #endregion
    }
}
